import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/screens/location/widget/permission_dialog.dart';
import 'package:get/get.dart';
import 'package:location/location.dart';

class LocationPermissionHelper {
  static void checkPermission(Function onTap) async {
    Location location = new Location();

    bool serviceEnabled;
    PermissionStatus permissionGranted;

    serviceEnabled = await location.serviceEnabled();
    if (!serviceEnabled) {
      serviceEnabled = await location.requestService();
      if (!serviceEnabled) {
        showCustomSnackBar('Location services are disabled.');
        checkPermission(onTap);
        return;
      }
    }

    permissionGranted = await location.hasPermission();
    if (permissionGranted == PermissionStatus.denied) {
      permissionGranted = await location.requestPermission();
      if (permissionGranted == PermissionStatus.denied) {
        showCustomSnackBar('You have to allow permission.');
        checkPermission(onTap);
        return;
      }
    }

    if (permissionGranted == PermissionStatus.deniedForever) {
      Get.dialog(PermissionDialog());
      return;
    }

    if (permissionGranted == PermissionStatus.granted) {
      onTap();
    }
  }
}
